package com.example.takeaway.utils;


import com.aliyun.tea.*;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teaxml.Client;

public class SMSUtils {


    /**
     * 使用AK&SK初始化账号Client
     * @param accessKeyId LTAI5tRnmuUkyLs1d7bVfa4u
     * @param accessKeySecret
     * @return Client
     * @throws Exception
     */
    public static com.aliyun.dysmsapi20170525.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // 必填，您的 AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 必填，您的 AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        // Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new com.aliyun.dysmsapi20170525.Client(config);
    }

    public static void sendSms(String phone,String code) throws Exception {
        // 获取环境变量中的 AccessKey 和 SecretKey
        String accessKeyId = "LTAI5tDzR3JKbfxxVA1JBUkA";
        String accessKeySecret = "cLYw6wYP7HKKzCDxz7uvUb5mFXmtzV";

        // 创建客户端实例
        com.aliyun.dysmsapi20170525.Client client = SMSUtils.createClient(accessKeyId, accessKeySecret);

        // 创建 SendSmsRequest 对象并设置参数
        com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
                .setPhoneNumbers(phone) // 替换成目标手机号码
                .setSignName("看黑马学习瑞吉外卖") // 替换成已注册并通过审核的签名名称
                .setTemplateCode("SMS_293990823") // 替换成你的短信模板代码
                .setTemplateParam("您的验证码为：{" + code + "}，请勿泄露于他人！"); // 替换成与模板对应的参数

        try {
            client.sendSmsWithOptions(sendSmsRequest, new com.aliyun.teautil.models.RuntimeOptions());
            System.out.println("短信发送成功！");
        } catch (Exception e) {
            System.out.println("短信发送失败：" + e.getMessage());
        }
    }


}
